package network;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import configs.Config;
import jmetal.core.Algorithm;
import jmetal.core.Operator;
import jmetal.core.Problem;
import jmetal.core.Solution;
import jmetal.core.SolutionSet;
import jmetal.operators.crossover.CrossoverFactory;
import jmetal.operators.mutation.MutationFactory;
import jmetal.operators.selection.SelectionFactory;
import jmetal.qualityIndicator.QualityIndicator;
import jmetal.util.JMException;
import jmetal.util.Ranking;
import util.Calc;

public class TestNetwork {
	

	public static void main(String[] args) {
		/*
		    RBFNetwork method = new RBFNetwork(2,4,1,RBFEnum.Gaussian);
		    MLDataSet dataSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL);
		    // train to 1%
		    EncogUtility.trainToError(method, dataSet, 0.01);
		    // evaluate
		    EncogUtility.evaluate(method, dataSet); */
		    
			jmetal.util.PseudoRandom.setRandomGenerator(new util.MyRandom(985));
		    
		    try {
		    	Config.LoadConfigs();
		    	
		    	Problem problem = Config.problem;
				Algorithm algorithm = Config.algorithm;
				
				Operator crossover;
				Operator mutation;
				Operator selection;
				
				HashMap<String,Object> parameters;
				QualityIndicator indicators = null;
				
				algorithm.setInputParameter("populationSize", Config.populationSize);
				algorithm.setInputParameter("maxEvaluations", Config.maxEvaluations);
							
				parameters = new HashMap<String,Object>();
				parameters.put("probability", 0.9);
				parameters.put("distributionIndex", 20.0);
				crossover = CrossoverFactory.getCrossoverOperator("SBXCrossover", parameters);
				
				parameters = new HashMap<String,Object>();
				parameters.put("probability", 1.0/problem.getNumberOfVariables());
				parameters.put("distributionIndex", 20.0);
				mutation = MutationFactory.getMutationOperator("PolynomialMutation", parameters);
				//mutation = new MutationJSF(parameters);
				
				parameters = null;
				selection = SelectionFactory.getSelectionOperator("BinaryTournament2", parameters);
				
				algorithm.addOperator("crossover", crossover) ;
				algorithm.addOperator("mutation", mutation) ;
				algorithm.addOperator("selection", selection) ;

				algorithm.setInputParameter("Indicators", indicators);
				
				SolutionSet population = algorithm.execute();
				population.printObjectivesToFile(Config.DIR+"Objectives");
				population.printVariablesToFile(Config.DIR+"Variables");
				
				//create solutions
				//Distance distance = new Distance();
				Ranking ranking = new Ranking(population);
				SolutionSet bestFront = ranking.getSubfront(0);
				
				ArrayList<double[]> points = Calc.pointsEmpty(bestFront);
				
				////////////////////////////////////////////////////////
				
				/* MLP */
		    	
				NeuralNetwork net = new NeuralNetwork();
				net.train(population);				
				
				SolutionSet resp = new SolutionSet(population.size()+3);
				Solution s;
				for (int i = 0; i< points.size(); i++) {
					double[] p = points.get(i);
					System.out.println(Arrays.toString(p).replace(",", "").replace("[", "").replace("]", "").replace(".", ","));
					s = net.calculateSolution(p);
					resp.add(s);
				}				
				net.finalizeNet();
				
				resp.printObjectivesToFile(Config.DIR+"ObjectivesNet");
				resp.printVariablesToFile(Config.DIR+"VariablesNet");
				
				/* RBF 
				
				NeuralNetwork netRBF = new MyRBFNetwork();
				netRBF.train(population);
				
				SolutionSet resp2 = new SolutionSet(population.size()+3);
				Solution s2;
				for (int i = 0; i< points.size(); i++) {
					double[] p = points.get(i);
					s2 = netRBF.calculateSolution(p);
					resp2.add(s2);
				}				
				netRBF.finalizeNet();
				
				resp2.printObjectivesToFile(Config.DIR+"ObjectivesNetRBF");
				resp2.printVariablesToFile(Config.DIR+"VariablesNetRBF");*/
				
				Config.EndConfigs();
				
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JMException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
	

	}

}
